package bootstrap

import "github.com/robfig/cron/v3"

type Crontab struct {
	Exp     string
	Handler func()
}

var Cron = make([]*Crontab, 0)

// 注册定时任务
func RegisterCrontab(exp string, f func()) {
	Cron = append(Cron, &Crontab{
		Exp:     exp,
		Handler: f,
	})
}

func StartCron() {
	var c = cron.New()
	for _, crontab := range Cron {
		if _, err := c.AddFunc(crontab.Exp, crontab.Handler); err != nil {
			//TODO 记录日志
		}
	}
	c.Start()
	defer c.Stop()
}
